package nckd.wnqjt.tmc.tda.plugin.form;

import kd.bos.bill.BillShowParameter;
import kd.bos.bill.OperationStatus;
import kd.bos.dataentity.entity.DynamicObject;
import kd.bos.dataentity.resource.ResManager;
import kd.bos.form.ShowType;
import kd.bos.form.control.events.ClickListener;
import kd.bos.form.control.events.ItemClickEvent;
import kd.bos.form.field.events.BeforeF7SelectEvent;
import kd.bos.form.field.events.BeforeF7SelectListener;
import kd.bos.orm.query.QCP;
import kd.bos.orm.query.QFilter;
import kd.bos.report.ReportList;
import kd.bos.report.plugin.AbstractReportFormPlugin;
import kd.bos.servicehelper.BusinessDataServiceHelper;
import java.util.*;

public class CasClosePeriodFormRptExtPlugin extends AbstractReportFormPlugin implements ClickListener, BeforeF7SelectListener {
    @Override
    public void beforeF7Select(BeforeF7SelectEvent beforeF7SelectEvent) {
        System.out.println("1");
    }

    @Override
    public void registerListener(EventObject e) {
        super.registerListener(e);
        this.addItemClickListeners(new String[]{"toolbarap"});
    }

    @Override
    public void itemClick(ItemClickEvent evt) {
        super.itemClick(evt);

        String key = evt.getItemKey();
        if ("nckd_closeapply".equals(key)) {
            ReportList control = (ReportList)this.getControl("reportlistap");
            int[] rowIdxs = control.getEntryState().getSelectedRows();
            if (rowIdxs.length != 1) {
                this.getView().showTipNotification(ResManager.loadKDString("请选择一行需要操作的数据。", "CasClosePeriodFormRpt_7", "fi-cas-report", new Object[0]));
                return;
            }
            for(int idx : rowIdxs) {
                DynamicObject rowData = control.getReportModel().getRowData(idx);
                if (rowData != null) {
                    String status = rowData.getString("status");
                    // 期间
                    DynamicObject curperiod = rowData.getDynamicObject("curperiod");
                    // 资金组织
                    DynamicObject org = rowData.getDynamicObject("org");
                    if(org == null){
                        this.getView().showTipNotification(ResManager.loadKDString("请选择资金组织。", "CasClosePeriodFormRpt_7", "fi-cas-report", new Object[0]));
                        return;
                    }
                    if(curperiod == null){
                        this.getView().showTipNotification(ResManager.loadKDString("请选择期间。", "CasClosePeriodFormRpt_7", "fi-cas-report", new Object[0]));
                        return;
                    }
                    if("c".equals(status) || "e".equals(status)) {
                        QFilter existFilter = new QFilter("org", QCP.equals, org.getPkValue())
                                .and(new QFilter("nckd_period", QCP.equals, curperiod.getPkValue()))
                                .and(new QFilter("billstatus", QCP.not_equals, "C"));
                        DynamicObject closeapplybill = BusinessDataServiceHelper.loadSingleFromCache("nckd_closeapplybill", existFilter.toArray());
                        if(closeapplybill != null){
                            this.getView().showTipNotification(ResManager.loadKDString("当前["+
                                    org.getString("name") +"]组织有未完成的结账申请单据号[" +
                                    closeapplybill.getString("billno") +"]，请完成后再操作",
                                    "CasClosePeriodFormRpt_7", "fi-cas-report", new Object[0]));
                            return;
                        }
                        // 核算组织 (根据资金组织对应的会计账簿-主账簿)
                        DynamicObject relateorg = null;
                        if(org != null) {
                            QFilter filter = new QFilter("o_entry.o_relateorg.fbasedataid", QCP.equals, org.getPkValue())
                                    .and(new QFilter("o_entry.o_app.number", QCP.equals, "cas"))
                                    .and(new QFilter("bookstype.number", QCP.equals, "100001"));
                            DynamicObject accountbook = BusinessDataServiceHelper.loadSingleFromCache("gl_accountbook", filter.toArray());
                            if(accountbook != null) {
                                relateorg = accountbook.getDynamicObject("org");
                            }
                        }
                        String errormsg = "c".equals(status) ? "结账失败：" : "检查不通过:";
                        for (int i = 1; i < 20; i++) {
                            DynamicObject nextData = control.getReportModel().getRowData(idx + i);
                            if(nextData != null && nextData.get("org") == null) {
                                String message = nextData.getString("message");
                                if(message != null && !"".equals(message) && !message.contains("检查通过")) {
                                    errormsg += message;
                                }
                            }else{
                                break;
                            }
                        }
                        BillShowParameter billshowparameter = new BillShowParameter();
                        Map<String, Object> map = new HashMap<>();
                        map.put("org", org);
                        map.put("relateorg", relateorg);
                        map.put("errormsg", errormsg);
                        map.put("curperiod", curperiod);
                        billshowparameter.setCustomParam("org", org);
                        billshowparameter.setFormId("nckd_closeapplybill");
                        billshowparameter.setStatus(OperationStatus.ADDNEW);
                        billshowparameter.setCustomParams(map);
                        billshowparameter.getOpenStyle().setShowType(ShowType.Modal);
                        this.getView().showForm(billshowparameter);
                    }else{
                        this.getView().showTipNotification(ResManager.loadKDString("结账结果为：检查不通过或者结账失败时才能发起结账申请。", "CasClosePeriodFormRpt_7", "fi-cas-report", new Object[0]));
                        return;
                    }
                }
            }
        }
    }

}
